﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Crank.Core
{
    public class TokenizerFactory
    {
        public ITokenProvider FromString(string script)
        {
            return
                new PreprocessingTokenizer(
                    new WhitespaceCompressingLineProvider(
                        new CommentStrippingLineProvider(
                            new StringLineProvider(script))));
        }

        public ITokenProvider FromFileName(string fileName)
        {
            Preprocessor.Process( "include", new string[] { fileName } );
            return
                new PreprocessingTokenizer(
                    new WhitespaceCompressingLineProvider(
                        new CommentStrippingLineProvider(
                            new FileLoadingLineProvider())));

        }
    }

    public static class TokenizerFactoryDataModelExtensions
    {
        public static void ParseString(this DataModel model, string script)
        {
            model.Parse(new TokenizerFactory().FromString(script));
        }

        public static void ParseFile(this DataModel model, string fileName)
        {
            model.Parse(new TokenizerFactory().FromFileName(fileName));
        }
    }
}
